import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.border.*;
/*
 * Created by JFormDesigner on Mon Nov 29 15:59:15 CST 2021
 */



/**
 * @author 123
 */
public class addScDialog extends JDialog {
    InitialParam initial = null;
    Connection conn = null;
    int flag = 1;
    public addScDialog(Window owner) {
        super(owner);
        initComponents();
        initialConn();
    }

    void initialConn(){
        try {
            initial = new InitialParam();
            conn = initial.conn;
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }

    void modifySc(){
        if (Sc.isNull()){

        }else{
            scno_textfield.setText(Sc.scno);
            sno_textfield.setText(Sc.sno);
            cno_textfield2.setText(Sc.cno);
            mark_textfield.setText(Sc.mark);
        }
    }

    void updateSc(String scno,String sno,String cno,String mark) throws SQLException {
        String sql = "update sc set scno=?,sno=?,cno=?,mark=? where scno = ?";
        if (conn==null){
            JOptionPane.showMessageDialog(addScDialog.this,"数据库未连接！","提示",JOptionPane.OK_OPTION);
        }else {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, scno);
            st.setString(2, sno);
            st.setString(3, cno);
            st.setString(4, mark);
            st.setString(5, scno);
            if (st.execute()) {
                System.out.println("失败");
                JOptionPane.showConfirmDialog(addScDialog.this, "修改失败！", "提示", JOptionPane.OK_OPTION);
            } else {
                System.out.println("成功");
                JOptionPane.showConfirmDialog(addScDialog.this, "修改成功！", "提示", JOptionPane.CLOSED_OPTION);
            }
        }
    }


    private void birth_textfieldFocusLost(FocusEvent e) {
        // TODO add your code here
    }

    private void birth_textfieldFocusGained(FocusEvent e) {
        // TODO add your code here
    }

    private void okButtonMouseClicked(MouseEvent e) {
        if (flag == 1){
            try{
                addSc(scno_textfield.getText(),sno_textfield.getText(),cno_textfield2.getText(),mark_textfield.getText());
            }catch (Exception e4){
                e4.printStackTrace();
                JOptionPane.showMessageDialog(addScDialog.this,"添加失败","提示",JOptionPane.OK_OPTION);
            }
        }else {
            try {
                updateSc(scno_textfield.getText(), sno_textfield.getText(), cno_textfield2.getText(), mark_textfield.getText());
            } catch (Exception e4) {
                e4.printStackTrace();
                JOptionPane.showMessageDialog(addScDialog.this, "修改失败", "提示", JOptionPane.OK_OPTION);
            }
        }


    }
    void addSc(String scno,String sno,String cno,String mark) throws SQLException {
        String sql = "insert into Sc(scNo,sno,cno,mark)" +
                " values (?,?,?,?)";
        if (conn==null){
            JOptionPane.showMessageDialog(addScDialog.this,"数据库未连接！","提示",JOptionPane.OK_OPTION);
        }else {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, scno);
            st.setString(2, sno);
            st.setString(3, cno);
            st.setString(4, mark);
            if (st.execute()) {
                System.out.println("失败");
                JOptionPane.showConfirmDialog(addScDialog.this, "添加失败", "提示", JOptionPane.OK_OPTION);
            } else {
                System.out.println("成功");
                JOptionPane.showConfirmDialog(addScDialog.this, "添加成功！", "提示", JOptionPane.CLOSED_OPTION);
            }
        }

    }


    private void cancelButtonMouseClicked(MouseEvent e) {
        // TODO add your code here
    }

    private void thisWindowActivated(WindowEvent e) {
        //判断是添加用户还是修改用户
        if (!Sc.isNull()){
            flag = 2; //修改用户
            modifySc();
        }else{
            flag =1;//添加用户
        }
    }

    private void thisWindowClosing(WindowEvent e) {
        Course.putNull();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        this2 = new JPanel();
        scno_textfield = new JTextField();
        label1 = new JLabel();
        label5 = new JLabel();
        sno_textfield = new JTextField();
        label6 = new JLabel();
        mark_textfield = new JTextField();
        label7 = new JLabel();
        label3 = new JLabel();
        cno_textfield2 = new JTextField();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u6210\u7ee9\u8868\u7ba1\u7406\u7cfb\u7edf");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                thisWindowActivated(e);
            }
            @Override
            public void windowClosing(WindowEvent e) {
                thisWindowClosing(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //======== this2 ========
                {
                    this2.setLayout(null);

                    //---- scno_textfield ----
                    scno_textfield.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
                    this2.add(scno_textfield);
                    scno_textfield.setBounds(65, 50, 95, scno_textfield.getPreferredSize().height);

                    //---- label1 ----
                    label1.setText("\u6210\u7ee9\u53f7:");
                    this2.add(label1);
                    label1.setBounds(20, 55, 50, label1.getPreferredSize().height);

                    //---- label5 ----
                    label5.setText("\u5b66\u53f7:");
                    this2.add(label5);
                    label5.setBounds(185, 50, 50, 17);
                    this2.add(sno_textfield);
                    sno_textfield.setBounds(225, 50, 95, 30);

                    //---- label6 ----
                    label6.setText("\u6210\u7ee9:");
                    this2.add(label6);
                    label6.setBounds(185, 120, 45, 17);
                    this2.add(mark_textfield);
                    mark_textfield.setBounds(225, 120, 95, 30);

                    //---- label7 ----
                    label7.setText(" ");
                    this2.add(label7);
                    label7.setBounds(new Rectangle(new Point(300, 200), label7.getPreferredSize()));

                    //---- label3 ----
                    label3.setText("\u8bfe\u7a0b\u53f7:");
                    this2.add(label3);
                    label3.setBounds(20, 120, 50, 17);
                    this2.add(cno_textfield2);
                    cno_textfield2.setBounds(70, 120, 90, 30);

                    {
                        // compute preferred size
                        Dimension preferredSize = new Dimension();
                        for(int i = 0; i < this2.getComponentCount(); i++) {
                            Rectangle bounds = this2.getComponent(i).getBounds();
                            preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                            preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                        }
                        Insets insets = this2.getInsets();
                        preferredSize.width += insets.right;
                        preferredSize.height += insets.bottom;
                        this2.setMinimumSize(preferredSize);
                        this2.setPreferredSize(preferredSize);
                    }
                }
                contentPanel.add(this2);
                this2.setBounds(0, 10, 370, 195);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("\u63d0\u4ea4");
                okButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        okButtonMouseClicked(e);
                    }
                });
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("\u5173\u95ed");
                cancelButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        cancelButtonMouseClicked(e);
                    }
                });
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel this2;
    private JTextField scno_textfield;
    private JLabel label1;
    private JLabel label5;
    private JTextField sno_textfield;
    private JLabel label6;
    private JTextField mark_textfield;
    private JLabel label7;
    private JLabel label3;
    private JTextField cno_textfield2;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
